<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let users = [
        { id: "john", name: "John Smith", age: 20 },
        { id: "ann", name: "Ann Smith", age: 24 },
        { id: "jack", name: "jack Smith", age: 24 },
      ];
      /* function groupById(arr) {
        let obj = {};
        arr.forEach((v) => {
          obj[v.id] = v;
        });
        return obj;
      } */
      function groupById(arr = []) {
        return arr.reduce(function (obj, v) {
          obj[v.id] = v;
          return obj;
        }, {});
      }
      let usersById = groupById(users); // 调用函数
      console.log(usersById);
    </script>
  </body>
</html>
